package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.heima.file.service.impl.MinIOTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author xiaquanen
 * @version 1.0
 * @project heima-leadnews
 * @date 2024/7/21 14:29:01
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper,WmMaterial> implements WmMaterialService{

    @Autowired
    private MinIOTemplate minIOTemplate;
    @Autowired
    private WmMaterialMapper wmMaterialMapper;



    /**
     * 分页查询
     *
     * @param wmMaterialDto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto wmMaterialDto) {
        //参数校验
        if (wmMaterialDto == null) {
            return ResponseResult.errorResult(500,"参数错误");
        }
        //组装查询条件
        //考虑是否是收藏
        //按照时间排序
        LambdaQueryWrapper<WmMaterial> wmMaterialLambdaQueryWrapper = Wrappers.<WmMaterial>lambdaQuery().eq(wmMaterialDto.getIsCollection() == 1, WmMaterial::getIsCollection, wmMaterialDto.getIsCollection())
                .orderByDesc(WmMaterial::getCreatedTime);
        //分页查询
        Page page=new Page<>(wmMaterialDto.getPage(),wmMaterialDto.getSize());
        //组装结果
        page(page,wmMaterialLambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult((int)page.getCurrent(),(int)page.getSize(),(int)page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    /**
     * 图片上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        try {
            //获取文件的原始名称
            String originalFilename = multipartFile.getOriginalFilename();
            //生成一个UUID
            String s = UUID.randomUUID().toString();
            //截取文件后缀
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            //要存入minio中的文件名称
            String name = s + substring;
            //长传过后获取url
            String url = minIOTemplate.uploadImgFile("", name, multipartFile.getInputStream());
            //将数据存放到数据库中
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUrl(url);
            wmMaterial.setType((short) 0);
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setUserId(1102);
            wmMaterial.setCreatedTime(new Date());
            //保存到数据库
            save(wmMaterial);
            //将路径返回给前端
            return ResponseResult.okResult(wmMaterial);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 添加收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult collect(Integer id) {
        WmMaterial wmMaterial = wmMaterialMapper.selectById(id);
        int collection = wmMaterial.getIsCollection() == 1 ? 0 : 1;
        wmMaterial.setIsCollection((short) collection);
        wmMaterialMapper.update(null, Wrappers.<WmMaterial>lambdaUpdate().eq(WmMaterial::getId,id).set(WmMaterial::getIsCollection,collection));
        return ResponseResult.okResult(wmMaterial);
    }



}